#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <assert.h>
#include "hash.h"
using namespace std;


HashTable Str;
void getCount(stringstream &ss)
{
    string word;
    while (ss >> word)
    {
        for(int i=0; i<word.size(); i++)
            if(word[i] >= 'A' && word[i] <='Z')
                word[i] = word[i] + 32;
        Node* it = Str.search(word);
        if (it == NULL)
            Str.insert(word, 1);
        else
            it->number++;
    }
}

string readTxt(string file)
{
    ifstream infile;
    infile.open(file.data());  
    assert(infile.is_open());   

    string s;
    while(getline(infile,s))
    {
        cout<<s<<endl;
    }
    infile.close();             
    return s;
}

int main(int argc, const char * argv[]) {
    string tmp, strf;
    ifstream file("C:/Users/alibaba.txt");
    while (getline(file, tmp))
    {
        strf.append(tmp);
        tmp.clear();
    }
    for (size_t i = 0; i < strf.length(); i++)
    {
        if (ispunct(strf[i]) && (strf[i] != 39))
            strf[i] = ' ';
    }
    stringstream ss(strf);
    getCount(ss);

    freopen("C:/Users/out.txt", "w", stdout);
    printf("word              times\n");
    printf("---------------------------------------------------\n");
    Str.display();
    Str.printmax20();
    return 0;
}
